#!/usr/bin/env python
# encoding: utf-8
import sys, re
from xml.sax.saxutils import escape
from xml.sax.saxutils import unescape

def qj2bj(s):
    if not isinstance(s, str):
        s = s.decode()
    if s == "":
        return s
    else:
        bj_s = ""
        for c in s:
            c_ord = ord(c)
            if c_ord >= 65281 and c_ord <= 65374:
                bj_s += chr(c_ord - 65248)
            elif c_ord == 12288:
                bj_s += ' '
            else:
                bj_s += c
    return bj_s

def ngram(words, order):
    words_len = len(words)
    result = [''.join(words[i:j]) for i in range(words_len) \
            for j in range(i + 1, i + 1 + order) if j <= words_len]
    return result

def unify_punctuation(s, isEscape=True):
    temp_s = re.sub(r"""
            ，|
            ？|
            ！|
            ；|
            ：|
            “|
            ”|
            ‘|
            ’|
            『|
            』|
            ﹄|
            ﹃|
            「|
            」|
            （|
            ）|
            ——|
            ──|
            —|
            《|
            》|
            〈|
            〉|
            【|
            】|
            ［|
            ］|
            ～|
            ｜|
            ・"""
            , puct_repl, s, flags=re.X)
    if isEscape:
        return xml_escape(temp_s)
    else:
        return temp_s

def xml_escape(s):
    return escape(s, {"\"" : "&quot;", "\'" : "&apos;"})

def xml_unescape(s):
    return unescape(s, {"&quot;" : "\"", "&apos;" : "\'"})

def puct_repl(matchobj):
    if matchobj.group(0) == '？':
        return '?'
    elif matchobj.group(0) == '！':
        return '!'
    elif matchobj.group(0) == '；':
        return ";"
    elif matchobj.group(0) == '：':
        return ":"
    elif matchobj.group(0) == '“':
        return "\""
    elif matchobj.group(0) == '”':
        return "\""
    elif matchobj.group(0) == '‘':
        return "\'"
    elif matchobj.group(0) == '’':
        return "\'"
    elif matchobj.group(0) == '『':
        return "\""
    elif matchobj.group(0) == '』':
        return "\""
    elif matchobj.group(0) == '﹃':
        return "\""
    elif matchobj.group(0) == '﹄':
        return "\""
    elif matchobj.group(0) == '「':
        return "\""
    elif matchobj.group(0) == '」':
        return "\""
    elif matchobj.group(0) == '﹁':
        return "\""
    elif matchobj.group(0) == '﹂':
        return "\""
    elif matchobj.group(0) == '（':
        return "("
    elif matchobj.group(0) == '）':
        return ")"
    elif matchobj.group(0) == '——':
        return "--"
    elif matchobj.group(0) == '──':
        return "--"
    elif matchobj.group(0) == '—':
        return "--"
    elif matchobj.group(0) == '《':
        return "<<"
    elif matchobj.group(0) == '》':
        return ">>"
    elif matchobj.group(0) == '〈':
        return "<"
    elif matchobj.group(0) == '〉':
        return ">"
    elif matchobj.group(0) == '【':
        return "["
    elif matchobj.group(0) == '】':
        return "]"
    elif matchobj.group(0) == '［':
        return "["
    elif matchobj.group(0) == '］':
        return "]"
    elif matchobj.group(0) == '～':
        return "~"
    elif matchobj.group(0) == '・':
        return "·"
    else:
        return matchobj.group(0)

class Pattern():
    end_of_sentence=r"""(
                        \?!"\)|
                        \?"\)|
                        !"\)|
                        。"\)|
                        \."\)|
                        \?!\)|
                        \?\)|
                        !\)|
                        。\)|
                        \.\)|
                        \?!"|
                        \?"|
                        !"|
                        。"|
                        \."|
                        \?!|
                        \?|
                        !|
                        。
                        )"""

def gen_ilist(s, l):
    ilist = []
    index = -1
    for short_c in s:
        exist = False
        ilist.append([])
        index += 1
        position = -1
        for long_c in l:
            position += 1
            if long_c == short_c:
                exist = True
                ilist[index].append(position)
        if not exist: return []
    return ilist

def ext_relation(ilist, begin_p, index):
    relation = []
    max_length = []
    min_length = []
    position_list = ilist[index]
    if index + 1 == len(ilist):
        #init reversely.
        for position in position_list:
            if position > begin_p:
                relation.append([position])
                max_length.append(0)
                min_length.append(1000)
    else:
        for position in position_list:
            if position > begin_p:
                t_relation, t_max_length, t_min_length = ext_relation(ilist, position, index + 1)
                for i in range(len(t_relation)):
                    if t_max_length[i] < t_relation[i][0] - position:
                        max_length.append(t_relation[i][0] - position)
                    else:
                        max_length.append(t_max_length[i])
                    if t_min_length[i] > t_relation[i][0] - position:
                        min_length.append(t_relation[i][0] - position)
                    else:
                        min_length.append(t_min_length[i])
                    t_relation[i].insert(0, position)
                    relation.append(t_relation[i])
    return relation, max_length, min_length

def get_min_avg_max(s, l, relation, max_length, min_length):
    max_i = 0
    min_i = 1000
    avg_i = len(s) / len(l)
    for i in range(len(relation)):
        #measure the num of characters between each position
        temp_max = max_length[i] - 1
        temp_min = min_length[i] - 1
        left_edge = relation[i][0]
        right_edge = len(l) - 1 - relation[i][-1]
        if left_edge != 0 and temp_max < left_edge:
            temp_max = left_edge
        if right_edge != 0 and temp_max < right_edge:
            temp_max = right_edge
        if left_edge != 0 and temp_min > left_edge:
            temp_min = left_edge
        if right_edge != 0 and temp_min > right_edge:
            temp_min = right_edge
        if max_i < temp_max: max_i = temp_max
        if min_i > temp_min: min_i = temp_min
    return min_i, avg_i, max_i

def ext_full_abbr_info(s1, s2):
    if s1 == "" or s2 == "":
        return 0, 1.0, 0.0, 1.0
    if len(s1) < len(s2):
        s = s1
        l = s2
    else:
        s = s2
        l = s1
    #print("short:", s, "long:", l)
    ilist = gen_ilist(s, l)
    #print("ilist:", ilist)
    if ilist == []:
        return 0, 1.0, 0.0, 1.0
    relation, max_length, min_length = ext_relation(ilist, -1, 0)
    #print("relation:", relation)
    #print("max_length:", max_length)
    #print("min_length:", min_length)
    if relation == []:
        return 0, 1.0, 0.0, 1.0
    min_i, avg_i, max_i = get_min_avg_max(s, l, relation, max_length, min_length)
    diff = len(l) - len(s)
    return 1, min_i / diff if diff else min_i, avg_i, max_i / diff if diff else max_i


if __name__ == '__main__':
    print(ext_full_abbr_info(sys.argv[1], sys.argv[2]))
